﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code.IDAL;
using Manager.App_Code.DAL;
using Manager.App_Code.DTO;
using Manager.App_Code;
using System.Linq;
using Manager.Common;
using System.Data.Linq;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_QuocGia : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public Frm_BarButton_QuocGia()
        {
            InitializeComponent();
        }

        public void Frm_BarButton_QuocGia_Load(object sender, EventArgs e)
        {
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.QuocGias);
            gridQuocGia.DataSource = db.QuocGias.ToList();
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_Country frmCountry = new frm_Country();
            frmCountry.ShowDialog();
            Frm_BarButton_QuocGia_Load(null, null);
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    IQuocGia iquocgia = new QuocGiaDAL();
                    int index = gridView1.FocusedRowHandle;
                    string maquocgia = gridView1.GetRowCellValue(index, MaQuocGia).ToString();
                    var qg = db.QuocGias.SingleOrDefault(i => i.MaQuocGia == maquocgia);
                    db.QuocGias.DeleteOnSubmit(qg);
                    db.SubmitChanges();
                    MessageBox.Show("Xóa thành công.");
                    Frm_BarButton_QuocGia_Load(null, null);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_QuocGia:butXoa_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Xóa bị lỗi!!!");
            }
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            int index = gridView1.FocusedRowHandle;
            var maquocgia = gridView1.GetRowCellValue(index, MaQuocGia).ToString();
            var qg = db.QuocGias.SingleOrDefault(i => i.MaQuocGia == maquocgia);
            if (qg != null)
            {
                frm_Country frm = new frm_Country(qg);
                frm.ShowDialog();
                Frm_BarButton_QuocGia_Load(null, null);
            }
            else
                MessageBox.Show("Vui lòng chọn quốc gia");
        }

        private void Frm_BarButton_QuocGia_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
                this.butThemMoi_Click(sender, e);
            if (e.KeyCode == Keys.F5)
                this.butHieuChinh_Click(sender, e);
            if (e.KeyCode == Keys.F7)
                this.butXoa_Click(sender, e);
        }
    }
}